﻿// Learn more about F# at http://fsharp.net

// Learn more about F# at http://fsharp.net

module DblpAuthor

open System
open System.IO
open System.Collections.Generic
open System.Text.RegularExpressions
open System.Net


let delDup file =
    let lines = File.ReadAllLines(file)
    let dict = Dictionary<string, bool> ()
    lines |> Array.map (fun n->
        match dict.TryGetValue(n) with
        | true, _ -> ()
        | false, _ -> 
            dict.Add(n, true)
            printfn "%s" n)  
            

let dictToList (d:Dictionary<string, bool> ) =
    let l = new System.Collections.Generic.List<string> ()
    for a in d do
        if String.length (a.Key) < 20 then
            l.Add(a.Key)
            //printfn "%A" a.Key
            
    l
    
let findCoauthor htmlName = 
    let secHeader = "bgcolor=\"#ffffcc\">20"
    let paperHeader = "<tr><td align=\"right\" valign=\"top\" class=\"c\" bgcolor=\"#"
    let paperReg = new Regex(">[0-9]*</td><td")
    let authorHeader = "<a href=\""
    let rec getAuthors (text:string) (pos:int) = 
        let rec next pos = 
            if text.[pos] = ':' || text.[pos] = '<' then
                text.[pos]
            else
                next (pos+1)
        
        let p = text.IndexOf(authorHeader, pos)
        //printfn "%A" (text.Substring(p))
        
        let p1 = text.IndexOf(">", p) + 1
        let p2 = text.IndexOf("</", p) 
        let authorName = text.Substring(p1, p2-p1)
        if next (p2+4) = ':' then
            [authorName]
        else 
            authorName::getAuthors text p2
    
    let textOrig = File.ReadAllText htmlName
    let text = textOrig.ToLower()
    
    let dict = new Dictionary<string, bool> ()
    
    let addToDict (authors: string list) =
        for a in authors do
            match dict.TryGetValue(a) with
            | true, x -> ()
            | false, _ -> dict.Add(a, true)
        
    let mutable pos = 0
    pos <- text.IndexOf(secHeader)
    //printfn "first pos = %A  %A" pos (text.Substring(pos, 100))
    while pos >= 0 do 
        let indSec = text.IndexOf(secHeader, pos)
        //printfn "pos = %A sec = %A" pos indSec
        //let indPaper = text.IndexOf(paperHeader, pos)
        let indPaper = 
            if paperReg.IsMatch(text, pos) then 
                let matches = paperReg.Match(text, pos)
                let mgroup = matches.Groups
                let m = mgroup.[0]
                m.Index
            else
                -1
        match (indSec, indPaper) with
            | (_, -1) -> pos <- -1
            | (-1, p) -> 
                let authors = getAuthors text p
                addToDict authors
                //printfn "%A" dict
                //printfn "authors = %A" authors
                pos <- p + 1
            | (p1, p2) when p1 < p2 -> // section first
                let year = text.Substring(p1 + String.length secHeader - 2, 4)
                //printfn "%A" year
                if int year >= 2007 then 
                    pos <- p1 + 1
                else 
                    pos <- -1
            | (p1, p2) -> // paper first
                let authors = getAuthors text p2
                //printfn "get authors = %A" authors
                addToDict authors
                //printfn "authors = %A" authors
                pos <- p2 + 1
        //printfn "pos = %A" pos
    dict |> dictToList
    
let downloadUrl(url:string) = 
    let req = HttpWebRequest.Create(url) :> HttpWebRequest
    req.UserAgent <- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
    req.Method <- "GET";
    req.AllowAutoRedirect <- true;
    req.MaximumAutomaticRedirections <- 4;
    req.MaximumAutomaticRedirections <- 4;
    let resp = req.GetResponse()
    let stream = resp.GetResponseStream()
    let reader = new StreamReader(stream)
    reader.ReadToEnd()
   
let getDblpUrl url = 
    let htmlStr = downloadUrl url
    findCoauthor htmlStr
    